/**
 * 好友列表
 */
class friendList extends eui.Component{
    private return:eui.Button;
    private addFriend:eui.Button;

    private list:eui.List;

    private _arrayData:eui.ArrayCollection;
    private _friendData:Array<any> = [];

    private _addFriendUI:friendAdd;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "friendListSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private inIt(){
        this.addEventListener("close",this.close,this);
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.dispatchEventWith("return");
        },this);
        this.addFriend.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(!this._addFriendUI){
                this._addFriendUI = new friendAdd();
            }else{
                this._addFriendUI.getList();
            }
            this.addChild(this._addFriendUI);
        },this);

        this._arrayData = new eui.ArrayCollection(this._friendData);
        this.list.dataProvider = this._arrayData;
        this.list.itemRenderer = friendListIR;
        this.list.addEventListener("delete",(evt:GameEvent)=>{
            let obj = evt.data;
            let _model:model = new model("是否删除该好友");
            _model.addEventListener("delete",()=>{
                    this.removeChild(_model);
                    let dataPost = {
                        "delUserId":obj.userId
                    }
                    socket.request("deleteFriend",dataPost);
            },this);
            this.addChild(_model);
            // let dataPost:any = {"delUserId":obj.userId}
            // socket.request("deleteFriend",dataPost);
        },this);
        Dispatcher.ins.addEventListener("friendList",this.assignValue,this);
        Dispatcher.ins.addEventListener("deleteFriend",this.getList,this);
        this.getList();
    }
    public getList(){
        socket.request("friendList");
    }
    public assignValue(evt:GameEvent):void{
        let obj = evt.data;
        let arr = obj.list;
        this._friendData = arr;
        this._arrayData = new eui.ArrayCollection(this._friendData);
        this.list.dataProvider = this._arrayData;
        this._arrayData.refresh();
    }
    private close(evt:GameEvent):void{
        this.removeChild(this._addFriendUI);
        if(evt.data == 1){
            this.getList();
        }
    }
}
class friendListIR extends eui.ItemRenderer{
    private maskBg:eui.Rect;
    private head:eui.Image;
    private delete:eui.Rect;
    private sex:eui.Image;
    private nick:eui.Label;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "friendListIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.head.mask = this.maskBg;
        this.delete.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
            evt.stopPropagation();
            this.parent.dispatchEventWith("delete",true,this.data);
        },this);
        let status = "";
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            status = "begin";
        },this);
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            status = "move";
        },this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(status !== "move"){
                Dispatcher.ins.dispatchEvent(new GameEvent("openOtherHome",{"isFriend":0,"userId":this.data.userId}));
            }
        },this);
    }
    protected dataChanged():void{
        if(this.data.sex == 1){
            this.sex.source = "icon_friend_male_png";
        }else{
            this.sex.source = "icon_friend_female_png";
        }
        if(this.nick.text.length > 14){
            this.nick.text = this.nick.text.substr(0,13) + "...";
        }
    }
}